#!/bin/bash

set -e

build_www_dir() {
  local webpacked_www=$PWD/builder/www
  local src_www=kasmweb

  if [ "$src_www" -nt "$webpacked_www" ]; then
    docker build -t kasmweb/www -f builder/dockerfile.www.build .
    docker run --rm -v $PWD/builder/www:/build kasmweb/www:latest
  fi
}

move_libjpeg_turbo_to_os_specific_dir() {
  chown $L_UID:$L_GID $shared_with_docker_dir/${os_codename}/*
  mkdir -p $PWD/builder/build/${os_codename}/
  mv $shared_with_docker_dir/${os_codename}/libjpeg-turbo*.deb \
    $PWD/builder/build/${os_codename}/
}

shared_with_docker_dir=${GITLAB_SHARED_DIND_DIR:-/tmp}

cd "$(dirname "$0")/.."
. builder/os_ver_cli.sh

build_www_dir

builder_image=kasmvncbuilder:$os_codename${build_tag_for_images}
docker build -t "$builder_image" \
  -f builder/dockerfile.${os}_${os_codename}${build_tag}.build .
mkdir -p builder/build
docker run -v $shared_with_docker_dir:/build -e BUILD_TAG="$build_tag" \
  -e KASMVNC_COMMIT_ID="$KASMVNC_COMMIT_ID" \
  --rm "$builder_image"

L_GID=$(id -g)
L_UID=$(id -u)
tarball_name="kasmvnc.${os}_${os_codename}.tar.gz"
tarball_name_with_build_tag="kasmvnc.${os}_${os_codename}${build_tag}.tar.gz"
chown $L_UID:$L_GID $shared_with_docker_dir/$tarball_name
mv $shared_with_docker_dir/$tarball_name \
  $PWD/builder/build/"$tarball_name_with_build_tag"

if [ "$build_tag" = "+libjpeg-turbo_latest" ]; then
  move_libjpeg_turbo_to_os_specific_dir
fi
